<?php
/**
 * 功能: 邀请赚钱
 * User: WD
 * Date: 2018/8/23 0023
 * Time: 10:18
 */

namespace app\admin\controller;


use app\base\controller\Power;
use think\Db;

import('phpqrcode/phpqrcode',EXTEND_PATH);
class Invite extends Power{
    protected $beforeActionList = [
        'first'=> ['except'=>'index,code_add'],

    ];
    protected function first(){
        $result = $this->testing();
    }
    /**
     * 邀请赚钱 列表
     */
    public function index(){
        $invite = Db::name('invite')->select();
        $arr = [];
        foreach($invite as $k=>$v){
            $arr[$k] = $v;
            $arr[$k]['img'] = array_filter(explode(',',$v['image']));
        }

        $this->assign('invite',$arr);
        $this->assign('empty','<tr class="text-c empty"><td colspan="7">暂无数据</td></tr>');
        return view('');
    }

    /**
     * 添加
     */
    public function code_add(){
        $id = intval(input('id'));
        if($id){
            $result = Db::name('invite')->where('id',$id)->find();
            $result['image'] = array_filter(explode(',',$result['image']));
            /*echo '<pre>';
            print_r($result);*/
            $this->assign('result',$result);
        }
       return $this->fetch('invite:code-add');
    }
    public function code_hold(){
        $title = trim(input('title'));
        $link  = trim(input('link'));
        $image  = trim(input('image'));
        if(empty($title)||empty($link)||empty($image)){
            return json(['msg'=>'请填写完整的数据','ok'=>false]);
        }
        $data = [
            'title'=>$title,
            'link'=>$link,
            'image'=>$image
        ];
        $result = Db::name('invite')->insert($data);
        if($result){
            return json(['msg'=>'添加成功','ok'=>true]);
        }else{
            return json(['msg'=>'添加失败','ok'=>false]);
        }
    }
    /**
     * 生成二维码
     */
    public function create(){
        $id   = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数','ok'=>false]);
        }
        $link = trim(input('link'));
        $code = json_decode(scerweima2($link,$id),true);

        if($code['ok']){
            $result = Db::name('invite')->where('id',$id)->update(['code'=>$code['msg']]);
            if($result){
                return json(['msg'=>'生成二维码成功','ok'=>true]);
            }else{
                return json(['msg'=>'生成二维码失败','ok'=>false]);
            }
        }else{
            return json(['msg'=>$code['msg'],'ok'=>false]);
        }


    }

    /**
     * 修改
     */

    public function code_save(){
        $id     = intval(input('id'));
        $title  = trim(input('title'));
        $link   = trim(input('link'));
        $image  = trim(input('image'));
        if(empty($title)||empty($link)||empty($image)){
            return json(['msg'=>'请填写完整的数据','ok'=>false]);
        }
        $data = [
            'title'=>$title,
            'link'=>$link,
            'image'=>$image
        ];
        $result = Db::name('invite')->where('id',$id)->update($data);
        if($result){
            return json(['msg'=>'修改成功','ok'=>true]);
        }else{
            return json(['msg'=>'修改失败','ok'=>false]);
        }
    }


}